<?php
            
use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 


/**
 * Author: cbw
 * Desc: 优惠券
 * Time: 2022/11/14 08:33
 * Email: 17854288794@163.com
 */
class Flag_Page extends PluginWebPage {

    public function main()
    {
        global $_G,$_P,$_S,$_C;
        $method = 'coupon_flag';
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $theme = Theme::getThemeById($themeid);
        $theme_bengin_time = date('Y-m-d H:i:s',$theme['begintime']);
        $theme_end_time = date('Y-m-d H:i:s',$theme['endtime']);

        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;
        $coupon_list_arr = CouponModel::getFormatCouponInfo($themeid);
        $target = isset($_P['target'])?$_P['target']:0;
        $get_type = isset($_P['get_type'])?$_P['get_type']:0;
        $show_type = isset($_P['show_type'])?$_P['show_type']:0;
        $used_type = isset($_P['used_type'])?$_P['used_type']:0;
        $status = isset($_P['status'])?$_P['status']:0;
        $record_id = isset($_P['record_id'])?$_P['record_id']:0;
        $coupon_id = isset($_P['coupon_id'])?$_P['coupon_id']:0;
        $uid = isset($_P['uid'])?$_P['uid']:0;
        if ($record_id){
//            $status = 2;
        }

        $is_presale = CouponModel::isPreSaleSoftMode($_S,$_C);


        $used_type_arr = $select_used_type_arr = CouponModel::couponArrByKey('used_type',$themeid);
        $user_flag_status_arr = CouponModel::userCouponFlagArrByKey('status',$themeid,$coupon_id);


        $select_used_type_arr = array_splice($select_used_type_arr,1);

        if (empty($_P['createtime']['start'])){
            $starttime =  $start_day =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
            $start_day =  date('Y-m-d H:i:s',$start_day);
        }else{
            $starttime = $_P['createtime']['start'];
            $start_day = date("Y-m-d H:i:s",$_P['createtime']['start']);
        }
        $endtime = !$_P['createtime']['end']?time():$_P['createtime']['end'];
        $end_day = empty($_P['createtime']['end']) ? date('Y-m-d H:i:s',time()): date("Y-m-d H:i:s",$_P['createtime']['end']) ;

        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $total = CouponModel::userCouponListData($themeid,$target,$show_type,$used_type,$get_type,$status,0,$search_data,$page_start,$page_size,$start_day,$end_day,$record_id,$coupon_id,$uid,true);
        if (checksubmit('export_submit', true)){

            $header = array(
                'uid' => 'UID','nickname' => '昵称', 'username' => '姓名', 'mobile' => '电话',
                'workername'=>'归属员工','name' => '优惠券名称', 'price' => '优惠金额', 'status' => '状态',
                'created_at'=>'更新时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";
//            for ($j = 1; $j <= $pagesize; $j++) {
                $page_limit = 2000;
                $pagesize = ceil($total/$page_limit);
                $ex_page = $_P['ex_page']?$_P['ex_page']:1;
                $page_start = ($ex_page - 1) * $page_limit;
                $list = CouponModel::userCouponListData($themeid,'','','','',$status,0,$search_data,$page_start,$page_limit,'','','',$coupon_id);

                if (!empty($list)) {
                    $size = ceil(count($list) / 500);
                    for ($i = 0; $i < $size; $i++) {
                        $buffer = array_slice($list, $i * 500, 500);
                        foreach ($buffer as $row) {
                            switch ($row['status']){
                                case 1:
                                    $row['status'] = '未使用';
                                    break;
                                case 2:
                                    $row['status'] = '已使用';
                                    break;
                                case 3:
                                    $row['status'] = '已失效';
                                    break;
                                default:
                                    $row['status'] = '未知状态';
                                    break;
                            }
                            if (!$row['workername']){
                                $row['workername']='无';
                            }
//                            $workername = '';
//                            if ($row['workerid']){
//                                $r = Worker::getWokerName($_S,$themeid,$row['workerid']);
//                                $workername=$r['workername']==1?'后台管理员':$r['workername'];
//                            }
//                            $row['workerid'] = $workername;

                            if ($row['nickname']){
                                $row['nickname'] = str_replace(',','',$row['nickname']);
                                $row['nickname'] = str_replace('"','',$row['nickname']);
                            }

                            foreach ($keys as $key) {
//                                if ($row['default_type']!=2){
                                $data[] = $row[$key];
//                                }
                            }
                            if (isset($data)){
                                $user[] = implode("\t ,", $data) . "\t ,";
                                unset($data);
                            }
                        }
                        $html .= implode("\n", $user)."\n";
                        unset($user);
                    }
                }
            $is_end = 0;
            if($ex_page>=$pagesize){
                $is_end = 1;
            }
//            }
            //新版本导出合并
            Util::WxJump($_S,$html,'用户优惠券信息',$themeid,$is_end,$pagesize);
        }
        $list = CouponModel::userCouponListData($themeid,$target,$show_type,$used_type,$get_type,$status,0,$search_data,$page_start,$page_size,$start_day,$end_day,$record_id,$coupon_id,$uid);
//        var_dump($list);die();
        $pager = pagination($total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/coupon/flag/main', $return_clicent);
    }

    public function delete()
    {
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $id = isset($_P['id'])?$_P['id']:0;
        if (!$id){
            show_json(100,['msg'=>'记录id缺失']);
        }
        $delete_res = db_delete(t_yx_coupon_user_flag,['themeid'=>$themeid,'id'=>$id]);
        if ($delete_res){
            show_json(200,['msg'=>'删除优惠券成功']);
        }
        show_json(100,['msg'=>'删除优惠券失败']);
    }
}